package com.gxuwz.zlc.controller;

import com.gxuwz.zlc.common.Result;
import com.gxuwz.zlc.entity.Assistant;
import com.gxuwz.zlc.entity.Audition;
import com.gxuwz.zlc.entity.User;
import com.gxuwz.zlc.service.AssistantService;
import com.gxuwz.zlc.service.AuditionService;
import com.gxuwz.zlc.service.StudentService;
import com.gxuwz.zlc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 助理
 */
@Controller
@RequestMapping("/assistant")
public class AssistantController {
    // 助理表
    @Autowired
    private AssistantService assistantService;
    // 学生表
    @Autowired
    private StudentService studentService;
    // 用户表
    @Autowired
    private UserService userService;
    // 面试信息
    @Autowired
    private AuditionService auditionService;

    /**
     * 查询所有助理信息
     * @return
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public List<Assistant> findAssistant(){
        List<Assistant> assistantList = assistantService.findAssistant();
        return assistantList;
    }

    /**
     * 技术人员
     * 根据助理编号查询一个助理
     * @param assistantId
     * @return
     */
    @GetMapping("/findOne/{assistantId}")
    @ResponseBody
    public Result get(@PathVariable String assistantId){
        Assistant assistantById = assistantService.findAssistantById(assistantId);
        // code=0,msg=成功
        return Result.success(assistantById);
    }

    /**
     * 实验技术人员
     * 添加助理信息、在user表中把学生的perms的"student"改为"assistant"
     * @param assistant
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Map addAssistant(@RequestBody Assistant assistant){
        Map<String,String> map = new HashMap<>();
        try {
            // 把信息添加进助理表
            assistantService.addAssistant(assistant);
            // user表中把学生的perms的"student"改为"assistant"
            User user = new User();
            user.setUsername(assistant.getStuId());
            user.setPerms("assistant");
            userService.updateUser(user);
            // 更改面试信息录入状态
            Audition audition = new Audition();
            audition.setIsInput("1");
            //audition.setStuId(assistant.getStuId());
            auditionService.updateAuditionByStuId(audition,assistant.getStuId());

            map.put("code","200");
            map.put("msg","添加成功");
        } catch (Exception e) {
            map.put("code","500");
            map.put("msg","添加失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 技术人员
     * 修改助理信息
     * @param assistant
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map updateAssistant(@RequestBody Assistant assistant){
       assistantService.updateAssistant(assistant);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("msg","修改成功");
        return map;
    }

    /**
     * 技术人员
     * 删除助理信息
     * @param assistantId
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map deleteAssistant(@RequestParam(name = "assistantId") String assistantId){
        assistantService.deleteAssistant(assistantId);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("mag","删除成功");
        return map;
    }

}
